import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Tree } from 'antd';

const { TreeNode } = Tree;

class TreeTest extends Component {
    static propTypes = {
        idKey: PropTypes.string,
        nameKey: PropTypes.string,
        childNameKey: PropTypes.string,
        data: PropTypes.array,
    }

    static defaultProps = {
        idKey: 'key',
        nameKey: 'title',
        childNameKey: 'children', // subList
        data: [],
    }


    renderTreeNodes = data => {
        const { childNameKey, nameKey, idKey } = this.props;
        return data.map(item => {
            if (item[childNameKey]) {
                return (
                    <TreeNode title={item[nameKey]} key={item[idKey]} data={item}>
                        {this.renderTreeNodes(item[childNameKey])}
                    </TreeNode>
                );
            }
            return <TreeNode key={item[idKey]} {...item} data={item} />;
        });
    }



    render() {
        const { data } = this.props;
        return (
            <Tree

            >
                {this.renderTreeNodes(data)}
            </Tree>
        );
    }
}

export default TreeTest;